package com.xk.school_device.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.xk.school_device.common.Result;
import com.xk.school_device.entity.rep.RePasswordReq;
import com.xk.school_device.entity.Users;
import com.xk.school_device.service.IUsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xk
 * @since 2025-04-16
 */
@RestController
@RequestMapping("/api/user")
public class UsersController {
    @Autowired
    private IUsersService usersService;

    /**
     * 登录
     * @param user
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody Users user){

        return usersService.login(user);
    }
//    @PostMapping("/register")
//    public Result register(@RequestBody Users user){
//
//        return usersService.register(user);
//    }

    /**
     * 注销
     * @param user
     * @return
     */
    @PostMapping("/logout")
    public Result logout(@RequestBody Users user){
        return usersService.logout(user);
    }
    /**
     * 测试权限
     */
    @PostMapping("/permission")
    public Result permission(){
        if (StpUtil.hasPermission("role_sys_admin")){
            return Result.success("测试权限成功");
        }
        return Result.success("权限不够");
    }

    /**
     * 修改用户信息
     */
    @PostMapping("/updatePhoneUsername")
    public Result update(@RequestBody Users user){
        return usersService.updateUserInfoById(user);
    }
/**
 * 更换头像
 */

    @PostMapping("/updateAvatar")
    public Result updateAvatar(@RequestParam("userId") String id, @RequestParam("file") MultipartFile file){
        return usersService.updateAvatar(id,file);
    }
    /**
     * 重置密码
     */
    @PostMapping("/restPassword")
    public Result restPassword(@RequestBody RePasswordReq rePasswordReq) {
        usersService.restPassword(rePasswordReq.getId(), rePasswordReq.getRePassword());
        return Result.success("重置成功");
    }
}
